import 'package:flutter/material.dart';

class ShareDataWidget extends InheritedWidget {
  const ShareDataWidget({
    @required this.data,
    Widget child,
  }) : super(child: child);

  final int data; //需要在子树中共享的数据，保存点击次数

  //定义一个便捷方法，方便子树中的widget获取共享数据
  static ShareDataWidget of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<ShareDataWidget>();
  }

  //这个方法将只会获取，而不产生联系
  static ShareDataWidget getElementForInheritedWidgetOfExactType(BuildContext context) {
    return context
        .getElementForInheritedWidgetOfExactType<ShareDataWidget>()
        .widget as ShareDataWidget;
  }

  //该回调决定当data发生变化时，是否通知子树中依赖data的Widget
  @override
  bool updateShouldNotify(ShareDataWidget oldWidget) {
    //如果返回true，则子树中依赖(build函数中有调用)本widget
    //的子widget的`state.didChangeDependencies`会被调用
    return oldWidget.data != data;
  }
}


// @override
// InheritedElement getElementForInheritedWidgetOfExactType<T extends InheritedWidget>() {
//   assert(_debugCheckStateIsActiveForAncestorLookup());
//   final InheritedElement ancestor = _inheritedWidgets == null ? null : _inheritedWidgets[T];
//   return ancestor;
// }
// @override
// InheritedWidget dependOnInheritedWidgetOfExactType({ Object aspect }) {
//   assert(_debugCheckStateIsActiveForAncestorLookup());
//   final InheritedElement ancestor = _inheritedWidgets == null ? null : _inheritedWidgets[T];
//   //多出的部分
//   if (ancestor != null) {
//     assert(ancestor is InheritedElement);
//     return dependOnInheritedElement(ancestor, aspect: aspect) as T;
//   }
//   _hadUnsatisfiedDependencies = true;
//   return null;
// }

  // @override
  // InheritedWidget dependOnInheritedElement(InheritedElement ancestor, { Object aspect }) {
  //   assert(ancestor != null);
  //   _dependencies ??= HashSet<InheritedElement>();
  //   _dependencies.add(ancestor);
  //   ancestor.updateDependencies(this, aspect);
  //   return ancestor.widget;
  // }


  //   @override
  // void notifyClients(InheritedWidget oldWidget) {
  //   for (Element dependent in _dependents.keys) {
  //     notifyDependent(oldWidget, dependent);
  //   }
  // }

